#include "ohos_init.h"
#include "cmsis_os2.h"
#include "/home/openharmony/base/iot_hardware/peripheral/interfaces/kits/iot_gpio.h"
#include "/home/openharmony/base/iot_hardware/peripheral/interfaces/kits/iot_uart.h"
#include "/home/openharmony/base/iot_hardware/peripheral/interfaces/kits/iot_errno.h"
#include "nlp.h"
#include "../KP_GPIO_LED/gpio_led2.h"
#include "../PWM/pwmfan.h"
#define LED_TASK_GPIO1 5
#define LED_INTERVAL_TIME 30
#define LED_TASK_STACK_SIZE 1024
#define LED_TASK_PRIO 25

#define UART_TASK_STACK_SIZE (1024 * 8)
#define UART_TASK_PRIO 25
#define UART_BUFF_SIZE 17
#define TASK_DELAY_2S 1000000

static void NLPTask(void){
    uint8_t uart_buff[UART_BUFF_SIZE] = { 0 };
    uint8_t *uart_buff_ptr = uart_buff;
    uint8_t ret;
    IotGpioDir gpioDir;
    IotGpioValue gpiovalue;
    
    while (1) {
        printf("=======================================\r\n");
        printf("*************Hi3861_demo**************\r\n");
        printf("=======================================\r\n");


        //读取串口信息
        memset_s(uart_buff, sizeof(uart_buff), 0, sizeof(uart_buff));
        IoTUartRead(LED_TASK_GPIO1, uart_buff, UART_BUFF_SIZE);

        printf("Uart1 read data:\n");
        for(int i = 0;i<16;i++){
            printf(" %02x ",*(uart_buff+i));
        }
        printf("\n");

        //判断口令 实现LED开关
        if(uart_buff[1]==4&&uart_buff[3]==0){
            isOpenLed=1;
        }
        else if(uart_buff[1]==1&&uart_buff[3]==0){
            isOpenPwmfan = 1;
        }
        else{
            isOpenPwmfan = 0;
        }
        usleep(TASK_DELAY_2S);
    }
}

static void NLPEntry(void){
    osThreadAttr_t attr;
    uint8_t ret;
    IotUartAttribute uart_attr = {

        // baud_rate: 9600
        .baudRate = 9600,

        // data_bits: 8bits
        .dataBits = 8,
        .stopBits = 1,
        .parity = 0,
    };

    //NULLptr
    if(&uart_attr==NULL){
        printf("kongzhizhen\n");
    }

    //初始化
    ret = IoTUartInit(LED_TASK_GPIO1, &uart_attr);
    if (ret != IOT_SUCCESS) {
        printf("Failed to init uart! Err code = %d\n", ret);
        return;
    }

    attr.name = "NLPTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = LED_TASK_STACK_SIZE;
    attr.priority = LED_TASK_PRIO;
    if(osThreadNew((osThreadFunc_t)NLPTask,NULL,&attr) == NULL){
        printf("[NLPTask] Falied to create NLPTask!\n");
    }
}

SYS_RUN(NLPEntry);